﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity;

namespace School.SystemManage.WebPages.MessageBoard
{
    public partial class MessageList : Base.PermissionBasePage
    {
        TechnologySchoolEntities db = new TechnologySchoolEntities();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                int total = db.MessageBoard.Count();
                AspNetPager1.RecordCount = total;
                BindData();
            }
        }

        private void BindData()
        {
            dl_message.DataSource = DAL.PagerHelper.GetPagerData("MessageBoard", "MsgID,Question,ClientName,QTime,Answer,IsChecked", "QTime",
                AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex, string.Empty);
            dl_message.DataBind();
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindData();
        }

        protected void lbtnDelete_Click(object sender, EventArgs e)
        {
            foreach (DataListItem item in dl_message.Items)
            {
                if (((CheckBox)item.FindControl("cbChecked")).Checked)
                {
                    long msgID = long.Parse(dl_message.DataKeys[item.ItemIndex].ToString());
                    var msg = db.MessageBoard.FirstOrDefault(m => m.MsgID == msgID);
                    if (msg != null)
                    {
                        db.DeleteObject(msg);
                    }
                }
            }
            db.SaveChanges();
            BindData();
        }

        protected void dl_message_CancelCommand(object source, DataListCommandEventArgs e)
        {
            dl_message.EditItemIndex = -1;
            BindData();
        }

        protected void dl_message_EditCommand(object source, DataListCommandEventArgs e)
        {
            dl_message.EditItemIndex = e.Item.ItemIndex;
            BindData();         
        }

        protected void dl_message_UpdateCommand(object source, DataListCommandEventArgs e)
        {
            try
            {
                long msgID = long.Parse(dl_message.DataKeys[e.Item.ItemIndex].ToString());
                string answer = HttpUtility.HtmlEncode(((TextBox)e.Item.FindControl("txbAnswer")).Text.Trim());
                bool isChecked = ((RadioButtonList)e.Item.FindControl("rbtlChecked")).SelectedValue == "1";

                var msg = db.MessageBoard.FirstOrDefault(m => m.MsgID == msgID);
                if (msg != null)
                {
                    msg.Answer = answer;
                    msg.IsChecked = isChecked;
                    msg.ATime = DateTime.Now;

                    if (db.SaveChanges() > 0)
                    {
                        dl_message.EditItemIndex = -1;
                        BindData();
                    }
                    else
                    {
                        lblMsg.Text = "更新失败！";
                    }
                }
                else
                {
                    lblMsg.Text = "更新失败，所操作的对象不存在！";
                }
            }
            catch (Exception ex)
            {
                lblMsg.Text = "更新发生异常，原因：" + ex.Message;
            }
        }

        protected void dl_message_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.EditItem)
            {
                System.Data.Common.DbDataRecord record = (System.Data.Common.DbDataRecord)e.Item.DataItem;
                ((RadioButtonList)e.Item.FindControl("rbtlChecked")).SelectedIndex = (bool)record["IsChecked"] == true ? 0 : 1;
            }
        }

        #region 操作权限控制

        public override void SetEditDisable()
        {
            base.SetEditDisable();

            lbtnDelete.Visible = false;
            foreach (DataListItem item in dl_message.Items)
            {
                if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
                {
                    LinkButton linkEdit = (LinkButton)item.FindControl("LinkButton1");
                    linkEdit.Visible = false;
                }
            }
        }

        public override void SetReviewDisable()
        {
            base.SetReviewDisable();
        }

        public override void SetViewDisable()
        {
            base.SetViewDisable();
        }

        #endregion
    }
}
